package leetcode;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-06-05 12:32
 * @ Description :
 */
public class MaxCommonPrefix
{
    public static void main(String[] args)
    {
        String[] strs = {"flower","flow","flight"};
        System.out.println(longestCommonPrefix(strs));
    }

    public static String longestCommonPrefix(String[] strs)
    {
        if (strs.length == 0)
        {
            return "";
        }
        String max = strs[0];
        for (int i = 1; i < strs.length; i++)
        {
            //求max和下一个字符串的最长公共子前缀
            //int maxCur = 0;
            int cur = 0;
            while (cur < max.length() && cur < strs[i].length())
            {
                if (max.charAt(cur) == strs[i].charAt(cur))
                {
                    cur++;
                }
                else
                {
                    break;
                }
            }
            max = max.substring(0,cur);
        }
        return max;
    }

}
